﻿Public Class ISCPacketCreator

    Public buf(ISCHandler.BufferSize) As Byte
    Public offset As Integer = 0
    Public handler As ISCHandler
    'BI = BytesIn
    Public Const BIInteger = 4
    Public Const BIFloat = 4
    Public Const BIShort = 2
    Public Const BILong = 8

    Public Sub New(ByVal handler As ISCHandler)
        Me.handler = handler
    End Sub

    Public Sub send()
        Dim buffer(ISCHandler.BufferSize) As Byte
        buffer(0) = &H5E
        buffer(1) = offset
        For i As Integer = 0 To offset Step i + 1
            buffer(5 + i) = buf(i)
        Next
        handler.workSocket.Send(buffer)
        'log.log(LogType.MSG_SENT)
        'For i As Integer = 0 To offset - 1 Step i + 1
        'Console.Write("{0:x2} ", buffer(i))
        'Next
        'Console.WriteLine()
    End Sub

    Public Sub addString(ByVal val As String)
        addInt(val.Length)
        offset += System.Text.Encoding.ASCII.GetBytes(val, 0, val.Length, buf, offset)
    End Sub

    Public Sub addInt(ByVal val As Integer)
        Dim byteArray(LoginServer.Handler.HeaderLen) As Byte
        Dim result As Integer = Nothing
        Dim ns As System.Globalization.NumberStyles = Globalization.NumberStyles.Any

        If CBool(val And Globalization.NumberStyles.HexNumber) Then
            ns = Globalization.NumberStyles.AllowHexSpecifier
        End If
        Integer.TryParse(val.ToString(), ns, Nothing, result)
        byteArray = BitConverter.GetBytes(val)
        For i As Integer = 0 To BIInteger - 1 Step i + 1
            buf(offset) = byteArray(i)
            offset += 1
        Next
    End Sub

    Public Sub addFloat(ByVal val As Single)
        Dim byteArray(LoginServer.Handler.HeaderLen) As Byte
        Dim result As Integer = Nothing
        Dim ns As System.Globalization.NumberStyles = Globalization.NumberStyles.Any

        If CBool(val And Globalization.NumberStyles.HexNumber) Then
            ns = Globalization.NumberStyles.AllowHexSpecifier
        End If
        Integer.TryParse(val.ToString(), ns, Nothing, result)
        byteArray = BitConverter.GetBytes(val)
        For i As Integer = 0 To BIFloat - 1 Step i + 1
            buf(offset) = byteArray(i)
            offset += 1
        Next
    End Sub

    Public Sub addShort(ByVal val As Short)
        Dim byteArray(LoginServer.Handler.HeaderLen) As Byte
        Dim result As Integer = Nothing
        Dim ns As System.Globalization.NumberStyles = Globalization.NumberStyles.Any

        If CBool(val And Globalization.NumberStyles.HexNumber) Then
            ns = Globalization.NumberStyles.AllowHexSpecifier
        End If
        Integer.TryParse(val.ToString(), ns, Nothing, result)
        byteArray = BitConverter.GetBytes(val)
        For i As Integer = 0 To BIShort - 1 Step i + 1
            buf(offset) = byteArray(i)
            offset += 1
        Next
    End Sub

    Public Sub addLong(ByVal val As Long)
        Dim byteArray(LoginServer.Handler.HeaderLen) As Byte
        Dim result As Integer = Nothing
        Dim ns As System.Globalization.NumberStyles = Globalization.NumberStyles.Any

        If CBool(val And Globalization.NumberStyles.HexNumber) Then
            ns = Globalization.NumberStyles.AllowHexSpecifier
        End If
        Integer.TryParse(val.ToString(), ns, Nothing, result)
        byteArray = BitConverter.GetBytes(val)

        For i As Integer = 0 To BILong - 1 Step i + 1
            buf(offset) = byteArray(i)
            offset += 1
        Next
    End Sub

    Public Sub addByte(ByVal val As Byte)
        buf(offset) = val
        offset += 1
    End Sub

    Public Shared Function getString(ByVal buf() As Byte, ByRef index As Integer) As String
        Dim len As Integer = BitConverter.ToInt32(buf, index)
        index += BIInteger
        Dim enc As New System.Text.ASCIIEncoding()
        Dim out As String = String.Empty
        out = enc.GetString(buf, index, len)
        index += out.Length
        Return out
    End Function

    Public Shared Function getInt(ByVal buf() As Byte, ByRef index As Integer) As Integer
        index += BIInteger
        Return BitConverter.ToInt32(buf, index - BIInteger)
    End Function

    Public Shared Function getFloat(ByVal buf() As Byte, ByRef index As Integer) As Single
        index += BIFloat
        Return BitConverter.ToSingle(buf, index - BIInteger)
    End Function

    Public Shared Function getShort(ByVal buf() As Byte, ByRef index As Integer) As Short
        index += BIShort
        Return BitConverter.ToInt16(buf, index - BIInteger)
    End Function

    Public Shared Function getLong(ByVal buf() As Byte, ByRef index As Integer) As Long
        index += BILong
        Return BitConverter.ToInt64(buf, index - BIInteger)
    End Function

    Public Shared Function getByte(ByVal buf() As Byte, ByRef index As Integer) As Byte
        index += 1
        Return buf(index - 1)
    End Function

End Class